//package com.atguigu.gulimail.member.configuration;
//
//import io.renren.common.validator.Assert;
//import lombok.SneakyThrows;
//import org.springframework.cloud.client.ServiceInstance;
//import org.springframework.cloud.client.discovery.DiscoveryClient;
//import org.springframework.http.HttpMethod;
//import org.springframework.http.client.ClientHttpRequest;
//import org.springframework.http.client.ClientHttpResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.web.client.RequestCallback;
//import org.springframework.web.client.ResponseExtractor;
//import org.springframework.web.client.RestClientException;
//import org.springframework.web.client.RestTemplate;
//import java.net.URI;
//import java.util.List;
//import java.util.Random;
//
///**
// * 重写doexecute方法，实现负载均衡功能
// */
//@Component
//public class DoexecuteRestTemplate extends RestTemplate {
//
//    private DiscoveryClient discoveryClient;
//
//    public DoexecuteRestTemplate(DiscoveryClient discoveryClient){
//        this.discoveryClient = discoveryClient;
//    }
//
////    public <T>T getInfo(URI url ){
////        RequestCallback requestCallback  = this.acceptHeaderRequestCallback();
////        ResponseExtractor<T> responseExtractor = this.responseEntityExtractor("json");
////        return doExecute(url,HttpMethod.POST,requestCallback,responseExtractor);
////    }
//
//    @SneakyThrows
//    @Override
//    protected <T> T doExecute(URI url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor) throws RestClientException {
//        Assert.isNull(url,"输入URI不能为空");
//        ClientHttpResponse reponse = null;
//        URI uri = replace(url);
//        ClientHttpRequest request = createRequest(uri,method);
//        reponse = request.execute();
//        return (responseExtractor != null ? responseExtractor.extractData(reponse) : null);
//    }
//
//    private URI replace(URI uri){
//        String s = uri.toString();
//        String[] split = s.split("//");
//        String[] serviceNameArray = split[1].split(":");
//        List<ServiceInstance> serviceNameList = discoveryClient.getInstances(serviceNameArray[0]);
//        Random random = new Random();
//        int nextInt = random.nextInt(serviceNameList.size());
//        ServiceInstance serviceInstance = serviceNameList.get(nextInt);
//        return  serviceInstance.getUri();
//    }
//}
